<template>
  <el-dialog
    width="500px"
    title="员工导入"
    :visible="showExcelDialog"
    @close="$emit('update:showExcelDialog', false)"
  >
    <el-row type="flex" justify="center">
      <div class="upload-excel">
        <input
          ref="excel-upload-input"
          class="excel-upload-input"
          type="file"
          accept=".xlsx, .xls"
          @change="uploadChange"
        >
        <div class="drop">
          <i class="el-icon-upload" />
          <span>
            <el-button type="text" @click="getExportTemplate">下载导入模板</el-button>
          </span>
          <span>将文件拖到此处或
            <el-button type="text" @click="handleUpload">点击上传</el-button>
          </span>
        </div>
      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <!-- update:props属性名，值 直接修改 .sync修饰符的属性值 -->
      <el-button size="mini" type="primary" @click="$emit('update:showExcelDialog', false)">取消</el-button>
    </el-row>
  </el-dialog>
</template>
<script>

import { getExportTemplate, uploadExcel } from '@/api/employee'
import FileSaver from 'file-saver'
export default {
  name: 'ImportExcel',
  props: {
    showExcelDialog: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    getExportTemplate() {
      this.$confirm('是否下载导入模板?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(async() => {
        this.$notify({
          title: '正在下载导入模板',
          message: '需要大概30秒',
          type: 'success',
          duration: 0
        })
        // 导出员工excel表
        const res = await getExportTemplate()
        // FileSaver.saveAs(blob对象, 文件名.文件类型)
        FileSaver.saveAs(res, '员工导入模板.xlsx')
        this.$nextTick(() => {
          this.$notify.closeAll()
          this.$notify({
            title: '下载成功',
            message: '下载成功请在浏览器下载文件里面查看',
            type: 'success'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消下载'
        })
      })
    },
    handleUpload() {
      // 获取文件对象 弹出文件选择框
      this.$refs['excel-upload-input'].click()
    },
    async uploadChange(event) {
      // 调用上传接口
      // 参数为 form-data 需要文件file
      // event.target.files[0] 获取文件对象
      console.log(event)
      const files = event.target.files // input文件列表
      if (files.length > 0) {
        // 大于0 说明有文件
        const data = new FormData()
        // files[0] // 文件对象
        data.append('file', files[0]) // 将文件的对象添加到form-data中
        try {
          await uploadExcel(data)
          // 成功
          // 关闭弹框
          this.$emit('update:showExcelDialog', false)
          this.$notify({
            title: '导入成功',
            message: 'excel数据导入成功',
            type: 'success'
          })
          // 重新加载数据
          this.$emit('uploadSuccess')
        } catch (e) {
          this.$notify({
            title: '导入失败',
            message: 'excel数据导入失败，请重试',
            type: 'error'
          })
        } finally {
          // 清空文件
          this.$refs['excel-upload-input'].value = null
        }
      }
    }

  }
}
</script>

<style scoped lang="scss">
.upload-excel {
  display: flex;
  justify-content: center;
  margin: 20px;
  width: 360px;
  height: 180px;
  align-items: center;
  color: #697086;
  .excel-upload-input {
    display: none;
    z-index: -9999;
  }
  .btn-upload,
  .drop {
    border: 1px dashed #dcdfe6;
    width: 100%;
    height: 100%;
    text-align: center;
    line-height: 160px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .drop {
    line-height: 40px;
    color: #bbb;
    i {
      font-size: 60px;
      display: block;
      color: #c0c4cc;
    }
  }
}
</style>
